package com.example.moonshine.redchange.Util;

import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.annotation.Encoding;

import java.util.Map;

public class HttpUtil {

    /**
     *  okhttp3 同步post请求
     * @param url  请求的url
     * @param map  请求的参数体
     * @return
     */
    public String PostHttp(final String url, final Map map,String header){

        final String[] result = {null};
        final HttpInfo info = HttpInfo.Builder()
                .addHead("Content-Type",header)
                .addParams(map)
                .setUrl(url)
                .setResponseEncoding(Encoding.UTF_8)
                .build();
        OkHttpUtil.getDefault(this).doPostSync(info);
        result[0] = info.getRetDetail();
        return result[0];
    }


    /**
     * okhttp3 同步get请求
     * @param url  请求链接
     * @param map  请求参数对象
     * @return
     */
    public String GetHttp(final String url, final Map map,String header){

        final String[] result = {null};
        final HttpInfo info = HttpInfo.Builder()
                .addHead("Content-Type",header)
                .addParams(map)
                .setUrl(url)
                .setResponseEncoding(Encoding.UTF_8)
                .build();
        OkHttpUtil.getDefault(this).doGetSync(info);
        result[0] = info.getRetDetail();
        return result[0];
    }
}
